//This script is meant for SHC releases, being a modified version of CZS + UEZ, adapted to contain only what is needed.

global u8 customZoneSelect.useAlternates
global u8 customZoneSelect.useMode1Alts = 1
global u8 customZoneSelect.alternateZoneSet = 0
global u8 customZoneSelect.foundAlternateZone

function bool SHCSplash.shouldShowUpAfterStartup()
{
	return true
}

//# post-update-hook
function void PostFrameUpdate()
{
	base.PostFrameUpdate()
	customZoneSelect.useMode1Alts = 1
	if global.zone == 0x05 && customZoneSelect.useAlternates == 1
	{
		System.loadExternalRawData("icz_tiles_primary_alter", 0x03D4296)
		System.loadExternalRawData("icz_blocks_primary_alter", 0x03D3FB6)
		System.loadExternalRawData("icz_chunks_primary_alter", 0x03D56A8)
		System.loadExternalRawData("icz1_palette_alter", 0x0A915C)
		System.loadExternalRawData("icz1_tiles_secondary_alter", 0x03D64C8)
		System.loadExternalRawData("icz1_blocks_secondary_alter", 0x03D5958)
		System.loadExternalRawData("icz1_chunks_secondary_alter", 0x03D844A)
	}
}

function bool fn00d458()
{
	if !(u8[A1] & 0x80)
	{
		if !(u16[A0 + 0x38] == 0 && objA0.value3b == 0)
		{
			D1.u16 = u16[A0 + 0x36]
			if (control.pad1.pressed & CONTROL_DOWN)
			{
				playSound(SFX_CLICK)
				if D1.u16 == 0x04 && customZoneSelect.useAlternates == 1
				{
					customZoneSelect.useAlternates = 0
					return 2
					++D1.u16
				}
				else customZoneSelect.useAlternates = 1
			}
			else if (control.pad1.pressed & CONTROL_UP)
			{
				playSound(SFX_CLICK)
				if D1.u16 == 0x04 && customZoneSelect.useAlternates == 0
				{
					customZoneSelect.useAlternates = 1
					return 2
					--D1.u16
				}
				else customZoneSelect.useAlternates = 0
			}

			u16[A0 + 0x36] = D1.u16
			objA0.compound.sprite1.animation.sprite = (level.framecounter.low & 0x10) ? 0x1a : 0
		}
	}
	return base.fn00d458()
}

//# address-hook(0x01c362) end(0x01c388)
function void fn01c362()
{
	base.fn01c362()

	if customZoneSelect.useAlternates == 1
	{
		Standalone.loadRawLevelData("%s_layout_alter", global.zone_act, 0xffff8000)
	}
}

function void LevelRingsTableBuilder.buildRings()
{
	base.LevelRingsTableBuilder.buildRings()
	if customZoneSelect.useAlternates == 1
	{
		u32 bytes = Standalone.loadRawLevelData("%s_rings_alter", global.zone_act, 0x814000)
	}
}

function void LevelObjectTableBuilder.buildObjects()
{
	base.LevelObjectTableBuilder.buildObjects()
	if customZoneSelect.useAlternates == 1
	{
		u32 bytes = Standalone.loadRawLevelData("%s_objects_alter", global.zone_act, 0x810000)
	}
}

function bool Standalone.onWriteToSpriteTable(s16 px, s16 py, u16 renderQueue)
{
	if customZoneSelect.useAlternates == 1
	{
		if (objA0.update_address == 0x02d95c) && (objA0.animation.sprite == 0x09)
		{	
			SpriteHandle titlecard = Renderer.addSpriteHandle("customZoneSelect.0x05_titlecard", objA0.position.x.u16, 96, 0x9ffe)
			titlecard.setFlags(0x40|0x80)
			return true
		}
	}
	return base.Standalone.onWriteToSpriteTable(px, py, renderQueue)
}

function void VDP.writeData16(u16 value)
{
	if (coldPeaksZone.forceCPZ)
	{
		if (value == 0xa567 && customZoneSelect.foundAlternateZone == 1) || (value > 0xa567 && value <= 0xa56b && global.game_mode == 0x4c)
		{
			value += 1
			if (value == 0xa56c)
			{
				value = 0xa562
				D1 = 0xa563
				VDP.setupVRAMWrite(D0 + u16(D7.u16 - 2) - 8)
				base.VDP.writeData16(D1.u16)
			}
			else if (u16[A3 + 0x36] + 2 > 10)
				D0.u16 -= 20
		}
		else if (value >= 0xa562 && value <= 0xa56b && u16[A3 + 0x36] + 2 > 10) || value == 0xa65c
		{
			if (value == 0xa65c)
				value = 0xa560 + u16[A3 + 0x36] - 6
			else D0.u16 += 2
		}
	}
	else if value >= 0xa562 && value <= 0xa56b && customZoneSelect.foundAlternateZone == 1 && global.game_mode == 0x4c
		value = 0xa587
	base.VDP.writeData16(value)
}

//# address-hook(0x00d96a) end(0x00d9f2)
function void fn00d96a()
{
	customZoneSelect.foundAlternateZone = false
	if customZoneSelect.useAlternates == 1 && D1.u8 == 4
	{
		customZoneSelect.foundAlternateZone = true
		SpriteHandle dataselpreview = Renderer.addSpriteHandle("customZoneSelect.icz_dataselect_preview", (objA0.position.x.u16 + getScreenExtend() - 208) - getScreenExtend() + 40, 16, 0x9ff9)
		dataselpreview.setFlags(0x40)
	}
	base.fn00d96a()
}

//shoutout to nabbup
function bool Standalone.onDrawVdpSpriteCompound(s16 px, s16 py, u8 size, u16 index, u16 renderQueue, u8 spriteCounter)
{
	if (index == 0x83BD || index == 0x93BD)
	{
		Renderer.drawVdpSprite(px, py, size, index, 0x9ffb)
		return true
	}
	return base.Standalone.onDrawVdpSpriteCompound(px, py, size, index, renderQueue, spriteCounter)
}

#if GAMEAPP >= 0x24060180

/*function void ActSelect.DrawMenu(u8 selectedEntry, u8 selectedZone, u8 selectedAct, s16 anchorX)
{
	base.ActSelect.DrawMenu(selectedEntry, selectedZone, selectedAct, anchorX)
	coldPeaksZone.ActSelectCZSControl()
	
}

function void coldPeaksZone.ActSelectCZSControl()
{
	if customZoneSelect.useAlternates == 1
		HUD.drawSprite("SHCColdPeaksZone.tip_actselect_mode1_1", 0, 0, 0xf010)
	if customZoneSelect.useAlternates == 0
		HUD.drawSprite("SHCColdPeaksZone.tip_actselect_mode1_2", 0, 0, 0xf010)

	if (control.pad1.state & CONTROL_DOWN && Input.buttonDown(BUTTON_R))
	{
		customZoneSelect.alternateZoneSet = 1
	}
	if (control.pad1.state & CONTROL_UP && Input.buttonDown(BUTTON_R))
	{
		customZoneSelect.alternateZoneSet = 0
	}
	if control.pad1.pressed & CONTROL_C && customZoneSelect.useAlternates == 0
		customZoneSelect.useAlternates = 1
	else if control.pad1.pressed & CONTROL_C && customZoneSelect.useAlternates == 1
		customZoneSelect.useAlternates = 0
}*/

function u8 ActSelect.GetNumZones()
{
	return base.ActSelect.GetNumZones() + 1
}

function u8 ActSelect.GetNumActs(u8 zoneNumber)
{
	if (zoneNumber > 4)
	{
		zoneNumber -= 1
	}
	return base.ActSelect.GetNumActs(zoneNumber)
}

function u16 ActSelect.TranslateZoneAct(u8 zoneNumber, u8 act)
{
	if (zoneNumber > 4)
	{
		if zoneNumber == 5
			customZoneSelect.useAlternates = 1
		else customZoneSelect.useAlternates = 0
		zoneNumber -= 1
	}
	return base.ActSelect.TranslateZoneAct(zoneNumber, act)
}

function string ActSelect.GetZoneName(u8 zoneNumber)
{
	if (zoneNumber > 4)
	{
		if zoneNumber == 5
			return "Cold Peaks Zone"
		zoneNumber -= 1
	}
	return base.ActSelect.GetZoneName(zoneNumber)
}

function string ActSelect.GetZonePreviewKey(u8 zone, u8 act, u8 image)
{
	if (zone > 4)
	{
		if zone == 5
			return stringformat("cold_peaks_zone_preview_%d%s", act + 1, (image % 2 == 0) ? "a" : "b")
		zone -= 1
	}
	return base.ActSelect.GetZonePreviewKey(zone, act, image)
}

#endif
